package xyz.xtt.schedule.service;

import java.math.BigInteger;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.web3j.protocol.core.methods.response.EthBlock.Block;

import com.google.common.collect.Lists;
import com.tongtong.middleware.client.BlockchainClient;
import com.tongtong.middleware.vo.params.AccountParams;
import com.tongtong.middleware.vo.params.bsn.RechargeRequest;
import com.tongtong.middleware.vo.params.nft.BurnNFTParams;
import com.tongtong.middleware.vo.params.nft.MintNFTParams;
import com.tongtong.middleware.vo.params.nft.TransferParams;
import com.tongtong.middleware.vo.response.AccountResponse;
import com.tongtong.middleware.vo.response.EstimateGasRes;
import com.tongtong.middleware.vo.response.TransactionMergeData;
import com.tongtong.share.client.ShareClient;
import com.tongtong.share.request.AssistantMessageRequest;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import xyz.xtt.common.response.ApiResult;
import xyz.xtt.pay.client.PayClient;
import xyz.xtt.pay.client.request.ConfirmPayOrderRequest;
import xyz.xtt.pay.client.request.FetchUnPaidOrdersRequest;
import xyz.xtt.pay.client.response.TblPayOrderDto;
import xyz.xtt.user.api.dto.TblUserFundRecordDto;
import xyz.xtt.user.api.dto.UpdateFundSettledRequest;
import xyz.xtt.user.client.UserClient;

/**
 * @author huqibo
 * @date 2025/03/26
 */
@Slf4j
@Service
public class InnerService {
	@Autowired
	private ShareClient shareClient;
	@Autowired
	private PayClient payClient;
	@Autowired
	private UserClient userClient;
	@Autowired
	private BlockchainClient blockchainClient;

	public Boolean sendAssistantMsg(AssistantMessageRequest request) {
		ApiResult<Boolean> response = shareClient.sendAssistantMsg(request);
		log.info("shareClient sendAssistantMsg response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, Boolean.FALSE);
	}

	public List<TblPayOrderDto> fetchUnPaidOrders(FetchUnPaidOrdersRequest fetchRequest) {
		ApiResult<List<TblPayOrderDto>> response = payClient.fetchUnPaidOrders(fetchRequest);
		log.info("payClient fetchUnPaidOrders response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, Lists.newArrayList());
	}

	public Boolean confirmPayStatus(ConfirmPayOrderRequest confirmRequest) {
		ApiResult<Boolean> response = payClient.confirmPayStatus(confirmRequest);
		log.info("payClient fetchUnPaidOrders response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, Boolean.FALSE);
	}

	public List<TblUserFundRecordDto> getNeedSettleRecords(Integer maxCount, Long lastId) {
		ApiResult<List<TblUserFundRecordDto>> response = userClient.getNeedSettleRecords(maxCount, lastId);
		log.info("payClient getNeedSettleRecords response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, Lists.newArrayList());
	}

	public Boolean updateFundSettled(UpdateFundSettledRequest request) {
		ApiResult<Boolean> response = userClient.updateFundSettled(request);
		log.info("userClient getNeedSettleRecords response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, Boolean.FALSE);
	}

	public BigInteger getBalance(String account) {
		ApiResult<BigInteger> response = blockchainClient.getBalance(account);
		log.info("blockchainClient getBalance response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, BigInteger.ZERO);
	}

	public Double balance() {
		ApiResult<Double> response = blockchainClient.balance();
		log.info("blockchainClient getBalance response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, Double.valueOf("0"));
	}

	public String recharge(RechargeRequest rechargeRequest) {
		ApiResult<String> response = blockchainClient.recharge(rechargeRequest);
		log.info("blockchainClient getBalance response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, StringUtils.EMPTY);
	}

	public AccountResponse createAndQueryAccount(AccountParams accountParams) {
		ApiResult<AccountResponse> response = blockchainClient.createAndQueryAccount(accountParams);
		log.info("blockchainClient getBalance response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	public String mintNFT(MintNFTParams mintNftParams) {
		ApiResult<String> response = blockchainClient.mintNFT(mintNftParams);
		log.info("blockchainClient mintNFT response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, StringUtils.EMPTY);
	}

	public String transfer(TransferParams transferParams) {
		ApiResult<String> response = blockchainClient.transfer(transferParams);
		log.info("blockchainClient transfer response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, StringUtils.EMPTY);
	}

	public String burnNFT(BurnNFTParams burnNftParams) {
		ApiResult<String> response = blockchainClient.burnNFT(burnNftParams);
		log.info("blockchainClient burnNFT response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, StringUtils.EMPTY);
	}

	public Block getBlockByNumber(BigInteger blockNumber) {
		ApiResult<Block> response = blockchainClient.getBlockByNumber(blockNumber);
		log.info("blockchainClient burnNFT response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	public TransactionMergeData getTransactionInfo(String hash) {
		ApiResult<TransactionMergeData> response = blockchainClient.getTransactionInfo(hash);
		log.info("blockchainClient getTransactionInfo response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	public BigInteger getBlockNumber() {
		ApiResult<BigInteger> response = blockchainClient.getBlockNumber();
		log.info("blockchainClient getBlockNumber response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, BigInteger.ZERO);
	}

	public EstimateGasRes estimateGas(String from, String to, String input) {
		ApiResult<EstimateGasRes> response = blockchainClient.estimateGas(from, to, input);
		log.info("blockchainClient estimateGas response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}
}
